package com.yanqu.road.server.cmd.monopolymarket.office;

import com.yanqu.road.entity.config.player.UserDecorationInfoResult;
import com.yanqu.road.entity.enums.eMonopolyMarketResType;
import com.yanqu.road.logic.config.GameConfig;
import com.yanqu.road.logic.pb.PlayerBasePb;
import com.yanqu.road.pb.monopolymarket.MonopolyMarketProto;
import com.yanqu.road.server.GameServer;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.monopolymarket.MonopolyMarketModule;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.gameplayer.module.player.DecorationModule;
import com.yanqu.road.server.gameplayer.module.player.PreReduceModule;
import com.yanqu.road.server.manger.MonopolyMarketCommonMgr;
import com.yanqu.road.server.manger.monopolymarket.MonopolyMarketMgr;
import com.yanqu.road.server.manger.player.UserMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.CrossProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.GameProtocol;
import com.yanqu.road.utils.property.Property;

import java.math.BigInteger;

/**
 * @Description
 * @Author cwq
 * @Data 2021/8/11 16:32
 */
@Cmd(code = GameProtocol.S_MONOPOLY_MARKET_OFFICE_ACTION, desc = "进入大宋榷署 抢夺/撤回位置")
public class MonopolyMarketOfficeActionCmd extends AbstractCommand {

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        MonopolyMarketProto.MonopolyMarketOfficeActionReqMsg req = MonopolyMarketProto.MonopolyMarketOfficeActionReqMsg.parseFrom(packet.getBytes());
        if(!MonopolyMarketMgr.canEnterSystem(player)){
            ret = GameErrorCode.E_SYSTEM_NO_OPEN;
            return;
        }
        if(!MonopolyMarketCommonMgr.isSystemOpen(MonopolyMarketMgr.group, eMonopolyMarketResType.OFFICE.getValue())){
            ret = GameErrorCode.E_MONOPOLY_MARKET_SYSTEM_NO_OPEN;
            return;
        }

        MonopolyMarketModule marketModule = player.getModule(MonopolyMarketModule.class);
        MonopolyMarketProto.MonopolyMarketOfficeActionCrossReqMsg.Builder toCrossMsg = MonopolyMarketProto.MonopolyMarketOfficeActionCrossReqMsg.newBuilder();
        if(req.getType() == 0){
            MonopolyMarketMgr.refreshUser(player,true,false);
            ret = MonopolyMarketMgr.checkTodayDispatchTimes(eMonopolyMarketResType.OFFICE.getValue(),marketModule.baseData.getOfficeRobTimes());
            if(ret != 0){
                return;
            }
            // 判断元宝是否足够
            int needConsumeGold = MonopolyMarketMgr.calSitDownConsumeGold(player);
            if(needConsumeGold > 0){
                Property property = new Property(GameConfig.GAME_MONEY_INGOTS, BigInteger.valueOf(needConsumeGold));
                if(!player.getModule(CurrencyModule.class).currencyIsEnough(property)){
                    ret = GameErrorCode.E_BAG_INGOTS_NO_ENOUGH;
                    return;
                }
                // 预扣除
                player.getModule(PreReduceModule.class).preReduce(property);
            }
            // 发生战斗数据
            UserDecorationInfoResult result = player.getModule(DecorationModule.class).getCurMaxDecorationInfo();
            toCrossMsg.setDecorationClass(result.decorationClass);
            toCrossMsg.setDecorationOverTime((int)(result.decorationOverTime));
            toCrossMsg.setPlayerInfo(PlayerBasePb.parsePlayerBaseTempMsg(UserMgr.getUserBaseInfo(player.getUserId(), GameServer.getInstance().getServerId())));
        }else {
            // 撤位置
        }
        toCrossMsg.setCtsMsg(req);
        player.sendPacket(CrossProtocol.C_CROSS_MONOPOLY_MARKET_OFFICE_ACTION, toCrossMsg);
    }

    @Override
    public void sendData(GamePlayer player) {
        if (ret != 0) {
            MonopolyMarketProto.MonopolyMarketOfficeActionRespMsg.Builder builder = MonopolyMarketProto.MonopolyMarketOfficeActionRespMsg.newBuilder();
            builder.setRet(ret);
            player.sendPacket(ClientProtocol.U_MONOPOLY_MARKET_OFFICE_ACTION, builder);
        }
    }

}
